//
//  RSSChannel.m
//  Nerdfeed
//
//  Created by Rafal Rebacz on 7/10/13.
//  Copyright (c) 2013 Rafal Rebacz. All rights reserved.
//

#import "RSSChannel.h"
#import "RSSItem.h"

@implementation RSSChannel

//@synthesize infoString, items, parentParserDelegate;

- (id)init
{
    self = [super init];

    if (self)
    {
        _items = [[NSMutableArray alloc] init];
    }
    
    return self;
}

- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
    attributes:(NSDictionary *)attributeDict
{
    NSLog(@"\t%@ found a %@ element", self, elementName);
    
    if([elementName isEqual:@"title"])
    {
        _currentString = [[NSMutableString alloc] init];
        [self setTitle:_currentString];
    }
    else if([elementName isEqual:@"description"])
    {
        _currentString = [[NSMutableString alloc] init];
        [self setInfoString:_currentString];
    }
    else if([elementName isEqual:@"item"])
    {
        RSSItem *entry= [[RSSItem alloc] init];
        [entry setParentParserDelegate:self];
        
        [parser setDelegate:entry];
        
        [_items addObject:entry];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [_currentString appendString:string];
}

- (void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName
{
    _currentString = nil;
    
    if([elementName isEqual:@"channel"])
    {
        [parser setDelegate:_parentParserDelegate];
    }
}

@end
